{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "┌ Info: Precompiling ReinforcementLearningAnIntroduction [02c1da58-b9a1-11e8-0212-f9611b8fe936]\n",
      "└ @ Base loading.jl:1273\n"
     ]
    }
   ],
   "source": [
    "using ReinforcementLearningAnIntroduction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "env = LeftRightEnv()\n",
    "ns, na = length(get_observation_space(env)), length(get_action_space(env))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "struct CollectValue <: AbstractHook\n",
    "    values::Vector{Float64}\n",
    "    CollectValue() = new([])\n",
    "end\n",
    "\n",
    "(f::CollectValue)(::PostEpisodeStage, agent, env, obs) = push!(f.values, agent.policy.π_target.learner.approximator(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = plot()\n",
    "for _ in 1:10\n",
    "    agent = Agent(\n",
    "        policy=OffPolicy(\n",
    "            VBasedPolicy(\n",
    "                learner=MonteCarloLearner(\n",
    "                    approximator=TabularApproximator(n_state=ns),\n",
    "                    kind=FIRST_VISIT,\n",
    "                    sampling=ORDINARY_IMPORTANCE_SAMPLING\n",
    "                    ),\n",
    "                mapping= (obs, V) -> [1]\n",
    "                ),\n",
    "            RandomPolicy(DiscreteSpace(1:na))\n",
    "            ),\n",
    "        trajectory=EpisodicCompactSARTSATrajectory()\n",
    "    )\n",
    "    hook = CollectValue()\n",
    "    run(agent, env, StopAfterEpisode(100000, is_show_progress=false),hook)\n",
    "    plot!(p, hook.values, xscale = :log10)\n",
    "end\n",
    "p"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Julia 1.3.0",
   "language": "julia",
   "name": "julia-1.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.3.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
